using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using OrbitOne.Elmah.Logic;

namespace OrbitOne.Elmah.Presenter
{
    public class RecordSearchRow : WebControl
    {
        public SearchRecord Record { get; set; }
        public string SearchFilterQueryString { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Record != null)
            {
                var link = new HyperLink
                               {
                                   NavigateUrl = "search.aspx?" + SearchFilterQueryString,
                                   ToolTip = "Drill down",
                                   CssClass = CssClass
                               };

                var column1 = new SearchImageColumn
                                  {
                                      CssClass = "searchimagecol",
                                      ImageCss = "expandicon",
                                      ToolTip = "Expand",
                                      FloatDirection = SearchImageColumn.Direction.Left,
                                      ImageSource = "updown.gif"
                                  };

                var column2 = new HtmlGenericControl("span");
                column2.Attributes.Add("class", "searchcol5");
                column2.InnerText = Record.Key;

                var column3 = new HtmlGenericControl("span");
                column3.Attributes.Add("class", "searchcol2 right");
                column3.InnerText = Record.Count.ToString();

                var column4 = new HtmlGenericControl("span");
                column4.Attributes.Add("class", "searchcol3 right");
                column4.InnerText = GetHowLongAgoString();

                link.Controls.Add(column1);
                link.Controls.Add(column2);
                link.Controls.Add(column3);
                link.Controls.Add(column4);

                link.RenderControl(writer);
            }
        }

        #region Private Methods
        private string GetHowLongAgoString()
        {
            var year = Record.MostRecent.Days / 365d;
            var month = Record.MostRecent.Days / 30d;
            var week = Record.MostRecent.Days / 7d;
            var day = Record.MostRecent.TotalDays;
            var hour = Record.MostRecent.TotalHours;
            var minute = Record.MostRecent.TotalMinutes;
            var second = Record.MostRecent.TotalSeconds;
            var millisecond = Record.MostRecent.Milliseconds;

            string retval;
            if (year >= 1)
            {
                retval = string.Format("{0} year{1} ago", (int)year, year > 1 ? "s" : string.Empty);
            }
            else if (month >= 1)
            {
                retval = string.Format("{0} month{1} ago", (int)month, month > 1 ? "s" : string.Empty);
            }
            else if (week >= 1)
            {
                retval = string.Format("{0} week{1} ago", (int)week, week > 1 ? "s" : string.Empty);
            }
            else if (day > 1)
            {
                retval = string.Format("{0} day{1} ago", (int)day, day > 1 ? "s" : string.Empty);
            }
            else if (hour > 1)
            {
                retval = string.Format("{0} hour{1} ago", (int)hour, hour > 1 ? "s" : string.Empty);
            }
            else if (minute > 1)
            {
                retval = string.Format("{0} minute{1} ago", (int)minute, minute > 1 ? "s" : string.Empty);
            }
            else if (second > 1)
            {
                retval = string.Format("{0} second{1} ago", (int)second, second > 1 ? "s" : string.Empty);
            }
            else
            {
                retval = string.Format("{0} millisecond{1} ago", (int)millisecond, millisecond > 1 ? "s" : string.Empty);
            }
            return retval;
        }
        #endregion
    }
}